clear
set more off
set matsize 5000

global lecmen    "\\intra\partages\au_amic2\SRCV\TABLES_INITIALES\INPUT\MENAGES"
global lecmensas "\\intra\partages\au_amic2\SRCV_AVRIL2021\TABLES_INITIALES_AVRIL2021\MENAGES"
global ecrmen    "\\intra\partages\au_amic2\SRCV_AVRIL2021\BASES_FEV2022_V5\MENAGES\autres_var"
global ecrstat   "\\intra\partages\au_amic2\SRCV_AVRIL2021\STAT_fev2022_V5\2008_2019_new"



/* 1 - EXTRACTION DES VARIABLES SELECTIONNéES */
/* a - fichier STATA 1ère livraison 2008-2017 */
foreach an of numlist 8  {
cd "$lecmen"
use menages0`an'_diff.dta, clear
keep idmenc_* men hx060 nactifs  nadult nenfants hx080 hx090 nivact chargr rembpa remvar ipcred ipelec ipimp iplog iploy  epargn equil puisec ///
     stoc mrfexo mrfliv mrflog mrfasv mrfaut mrfval
	 gen annee_SRCV = 200`an'
order men annee_SRCV 
cd "$ecrmen"
save autres_men_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 9  {
cd "$lecmen"
use menages0`an'_diff.dta, clear
keep idmenc_* men hx060 nactifs  nadult nenfants hx080 hx090 nivactb chargr rembpa remvar ipcred ipelec ipimp iplog iploy  epargn equil puisec ///
     stoc mrfexo mrfliv mrflog mrfasv mrfaut mrfval
	 gen annee_SRCV = 200`an'
order men annee_SRCV 
cd "$ecrmen"
save autres_men_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 10 / 17 {
cd "$lecmen"
use menages`an'_diff.dta, clear
keep idmenc_* men hx060 nactifs  nadult nenfants hx080 hx090 nivactb chargr rembpa remvar ipcred ipelec ipimp iplog iploy  epargn equil puisec ///
     stoc mrfexo mrfliv mrflog mrfasv mrfaut mrfval
gen annee_SRCV = 20`an'
order men annee_SRCV 
cd "$ecrmen"
save autres_men_`an'.dta, replace
display `an'
count 
}

/* b - fichier SAS 2nde livraison 2018-2019 */
foreach an of numlist 18 / 19 {
cd "$lecmensas"
use menages_20`an'.dta, clear
keep IDMENC_* men HX060 nactifs  nadult nenfants HX080 HX090 nivactb chargr rembpa remvar ipcred ipelec ipimp iplog iploy  epargn equil puisec ///
     stoc mrfexo mrfliv mrflog mrfasv mrfaut mrfval
gen annee_SRCV = 20`an'
order men annee_SRCV 
cd "$ecrmen"
save autres_men_`an'.dta, replace
display `an'
count 
}


/* 2 - CHANGEMENT DE NOM DES VARIABLES */
foreach an of numlist 8 / 17 {
cd "$ecrmen"
use autres_men_`an'.dta, clear
rename hx060 HX060
rename hx080 HX080
rename hx090 HX090
rename idmenc_* idmenc
save autres_men_`an'.dta, replace
}

foreach an of numlist 18 / 19 {
cd "$ecrmen"
use autres_men_`an'.dta, clear
rename IDMENC_* idmenc
save autres_men_`an'.dta, replace
}


/* 3 - EMPILEMENT DES TABLES */
clear 
use "$ecrmen\autres_men_8.dta", clear
append using 	"$ecrmen\autres_men_9.dta" "$ecrmen\autres_men_10.dta" "$ecrmen\autres_men_11.dta" ///
				"$ecrmen\autres_men_12.dta" "$ecrmen\autres_men_13.dta" "$ecrmen\autres_men_14.dta" ///
				"$ecrmen\autres_men_15.dta" "$ecrmen\autres_men_16.dta" "$ecrmen\autres_men_17.dta" ///
				"$ecrmen\autres_men_18.dta" "$ecrmen\autres_men_19.dta" 
count /* 134 473 */
order idmenc annee_SRCV men
sort idmenc annee_SRCV men				
save "$ecrmen\autres_menages_2008_2019.dta", replace


/* 4 - MISE EN FORME DE NIVACT_NEW NIVACT_NEW_2 ET IPLOY_NEW */
/* a - MISE EN FORME DE NIVACT_NEW */
clear
use "$ecrmen\autres_menages_2008_2019.dta", clear
tab nivact annee_SRCV
gen nivact_new = nivact if annee_SRCV == 2008
sort annee_SRCV
order annee_SRCV nivact nivactb nivact_new
* 2008
replace nivact_new = "6" if nivact == "1" 
replace nivact_new = "5" if nivact == "2" 
replace nivact_new = "4" if nivact == "3" 
replace nivact_new = "3" if nivact == "4" 
replace nivact_new = "2" if nivact == "5" 
replace nivact_new = "1" if nivact == "6" 
* de 2009 à 2019
replace nivact_new = nivactb if nivact_new == "" 
tab nivact_new annee_SRCV
save "$ecrmen\autres_menages_2008_2019_def.dta", replace

/* b - MISE EN FORME DE NIVACT_NEW_2 */
clear
use "$ecrmen\autres_menages_2008_2019_def.dta", clear
gen nivact_new_2 = ""
* 2008
replace nivact_new_2 = "1" if nivact == "6" 
replace nivact_new_2 = "2" if nivact == "5" 
replace nivact_new_2 = "3" if nivact == "4" 
replace nivact_new_2 = "4" if nivact == "3" 
replace nivact_new_2 = "5" if nivact == "2" 
replace nivact_new_2 = "5" if nivact == "1" 
* de 2009 à 2019
replace nivact_new_2 = "1" if nivactb == "1" 
replace nivact_new_2 = "2" if nivactb == "2" 
replace nivact_new_2 = "3" if nivactb == "3" 
replace nivact_new_2 = "4" if nivactb == "4" 
replace nivact_new_2 = "5" if nivactb == "5" 
replace nivact_new_2 = "5" if nivactb == "6" 

tab nivact annee_SRCV
tab nivactb annee_SRCV
tab nivact_new annee_SRCV
tab nivact_new_2 annee_SRCV
drop nivact nivactb
save "$ecrmen\autres_menages_2008_2019_def.dta", replace

/* c - MISE EN FORME DE IPLOY_NEW */
clear
use "$ecrmen\autres_menages_2008_2019_def.dta", clear
tab iploy annee_SRCV
gen iploy_new = iploy 
sort annee_SRCV
order annee_SRCV iploy iploy_new
replace iploy_new = "4" if iploy == "4" | iploy == "" 
tab iploy_new annee_SRCV
save "$ecrmen\autres_menages_2008_2019_def.dta", replace

/* d - CREATION DE YEAR */
clear
use "$ecrmen\autres_menages_2008_2019_def.dta", clear
gen year = annee_SRCV - 1
save "$ecrmen\autres_menages_2008_2019_def.dta", replace


/* 5 - MISE EN FORME AVANT EMPILEMENT : TRANSFORMATION DES VARIABLES CARACTERE EN NUMERIQUE */
clear
use "$ecrmen\autres_menages_2008_2019_def.dta", clear

foreach var of varlist nivact_new nivact_new_2 chargr rembpa remvar ipcred ipelec ipimp iplog iploy iploy_new  epargn equil puisec stoc mrfexo mrfliv mrflog mrfasv mrfaut mrfval {
tab `var', missing
destring `var', replace
tab `var', missing
}
sort men annee_SRCV year
order men annee_SRCV idmenc year HX060 HX090 HX080 nadult nactifs nenfants nivact_new nivact_new_2 chargr rembpa remvar ///
      ipcred ipelec  ipimp  iplog iploy iploy_new epargn equil puisec ///
	  stoc mrfexo mrfliv mrfasv mrflog mrfval mrfaut
save "$ecrmen\autres_menages_2008_2019_def.dta", replace
